package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shopcar/api/request"
	"shopcar/internal/dao"
	"shopcar/internal/model"
)

func ShopAdd(c *gin.Context) {
	var req request.ShopAdd
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  err.Error(),
			"data": req,
		})
		return
	}
	var shop model.Shop
	dao.GetOneByFields(&model.Shop{Name: req.Name}, &shop)
	if shop.Id != 0 {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "当前商品已存在",
			"data": nil,
		})
		return
	}
	result := dao.Create(&model.Shop{
		Name:   req.Name,
		Price:  req.Price,
		BoosId: c.GetInt("boosId"),
		Info:   req.Info,
		Num:    req.Num,
	})
	if !result {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "添加失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": nil,
	})

}

func Update(c *gin.Context) {
	var req request.ShopUpdate
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  err.Error(),
			"data": req,
		})
		return
	}
	var shop model.Shop
	dao.GetOneByFields(&model.Shop{Id: uint(req.Id)}, &shop)
	if shop.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "当前商品不存在",
			"data": nil,
		})
		return
	}
	result := dao.Update(&model.Shop{
		Id:    uint(req.Id),
		Name:  req.Name,
		Price: req.Price,
		Info:  req.Info,
		Num:   req.Num,
	})
	if !result {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "修改失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "修改成功",
		"data": nil,
	})
}

func ShopList(c *gin.Context) {
	var req request.ShopList
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	var shop []model.Shop
	dao.GetListByFields(&model.Shop{
		BoosId: int(req.BoosId),
	}, &shop)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": shop,
	})
	return
}

func ShopPage(c *gin.Context) {
	var req request.ShopPage
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	pageDto := dao.PageDto{
		Page:     uint(req.Page),
		PageSize: uint(req.PageSize),
		OrderBy:  "created_at desc",
	}

	var shops []model.Shop
	paginate, _ := dao.Paginate(&model.Shop{
		Name:   req.Name,
		Price:  req.Price,
		BoosId: int(req.BoosId),
		Info:   req.Info,
		Num:    req.Num,
	}, pageDto, &shops)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": paginate,
	})

}

func Detail(c *gin.Context) {
	var req request.ShopDetail
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	var shop model.Shop
	dao.GetOneByFields(&model.Shop{Id: uint(req.Id)}, &shop)
	if shop.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10000,
			"msg":  "当前商品不存在",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": shop,
	})
}
